﻿using System;


namespace advadev.Windows.DragDrop.Demo
{

    /// <summary>
    /// Contains the drag drop commands
    /// </summary>
    static public class DragDropCommands
    {

        /// <summary>for internal use</summary>
        static private DropCommand _decreaseCounterCommand;
        /// <summary>for internal use</summary>
        static private DropCommand _increaseCounterCommand;


        /// <summary>
        /// Gets the decrease counter command
        /// </summary>
        /// <remarks>
        /// Expects a demo model item as source parameter
        /// </remarks>
        static public DropCommand DecreaseCounterCommand
        {
            get
            {
                if (_decreaseCounterCommand == null)
                {
                    _decreaseCounterCommand = new DropCommand(decreaseCounterExecuted, decreaseCounterCanExecute);
                }
                return _decreaseCounterCommand;
            }
        }

        /// <summary>
        /// Gets the increase counter command
        /// </summary>
        /// <remarks>
        /// Expects a demo model item as target parameter
        /// </remarks>
        static public DropCommand IncreaseCounterCommand
        {
            get
            {
                if (_increaseCounterCommand == null)
                {
                    _increaseCounterCommand = new DropCommand(increaseCounterExecuted, increaseCounterCanExecute);
                }
                return _increaseCounterCommand;
            }
        }


        #region decrease counter command handler
        /// <summary>
        /// Handles the "CanExecute" event of the decrease counter command
        /// </summary>
        /// <param name="sender">
        /// Sender of the event
        /// </param>
        /// <param name="args">
        /// Event argument
        /// </param>
        static private bool decreaseCounterCanExecute(object sender, DropCommandParameter args)
        {
            DemoModelItem item = args.DropSourceParameter as DemoModelItem;
            return (item != null && item.Counter > 0 && args.DropTarget != null);
        }

        /// <summary>
        /// Handles the "Executed" event of the decrease counter command
        /// </summary>
        /// <param name="sender">
        /// Sender of the event
        /// </param>
        /// <param name="args">
        /// Event argument
        /// </param>
        static private void decreaseCounterExecuted(object sender, DropCommandParameter args)
        {
            DemoModelItem item = args.DropSourceParameter as DemoModelItem;
            if (item != null && item.Counter > 0 && args.DropTarget != null)
            {
                item.Counter--;
            }
        }
        #endregion decrease counter command handler


        #region increase counter command handler
        /// <summary>
        /// Handles the "CanExecute" event of the increase counter command
        /// </summary>
        /// <param name="sender">
        /// Sender of the event
        /// </param>
        /// <param name="args">
        /// Event argument
        /// </param>
        static private bool increaseCounterCanExecute(object sender, DropCommandParameter args)
        {
            DemoModelItem item = args.DropTargetParameter as DemoModelItem;
            return (item != null && item.Counter < 15);
        }

        /// <summary>
        /// Handles the "Executed" event of the increase counter command
        /// </summary>
        /// <param name="sender">
        /// Sender of the event
        /// </param>
        /// <param name="args">
        /// Event argument
        /// </param>
        static private void increaseCounterExecuted(object sender, DropCommandParameter args)
        {
            DemoModelItem item = args.DropTargetParameter as DemoModelItem;
            if (item != null && item.Counter < 15)
            {
                item.Counter++;
            }
        }
        #endregion increase counter command handler

    }

}
